<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/23
 * Time: 11:40
 */

namespace Home\Model;
use Common\Library\BusinessException;
use Common\Library\Constant;

use Think\Cache\Driver\Redis;
use Think\Log;
use Think\Model;

class YibaoModel
{
    #商户编号：
    public $merchantno = '10015653668';
#商户私钥：
    public $merchantPrivateKey ='MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAILJcnfsH3+b/CwGkFi5Zcw0220kodV5BtXRACZ5X/+9QUVi5GUroXzLyawxXqhqDFg4zQUCtptTASRW4OzJtJP8PocABSsmxc4bVbeQKlrdI/9fIXVlWZYXkeMuXfdL6g/RY0nsccF7wugaeuZqnEC0MLQWsi6GH4y+a33vtjZbAgMBAAECgYBPLZ37NBwtqUWVcbPZwwP6CNX+0XqYPlkBATLSGBEiRnVJiIE1LqDpaIaDlK/1zZ4Dmv+j1OywRCQg1+f78nE7cqjR07MmCQpOF9sOlOzwTEJ6kmUh0KtoSnRcfH/l3b2kbaMT8aS7Se09P13K1s8hh8dGu9KaX4lL4kBH6UMagQJBAOap2f5IGBjVpRqih2AyiL8RzRAyLNkBcZmZO9bYqKJOtbv5WguC4+1VQAYbGJ5DpxNIWC0eT5mf6t66kpugSAsCQQCRJxz9KgDVAEvh9DYZBuKnQQwoK+G2nJT7UljTPzHSv+jJW5O1uW9IbkgDrpWvepakSI8Y+vAf6Y5xR2XcIazxAkAH2ipgnKWtWsCHGpdPMAk1r+5yKbMbJBBw8SV3V+LNu642GvP0xlzp/Gf0jUGWSalLX8AYsRp2mVoOgP1yrM8nAkBSuBuJGrrGqBiWd/msYYQhFKy85+UFkE5GuzLrkAPwunUK6ylOUpLckBxyHLgo/I+Ep+UEs1zMkukeHTYrMPXBAkA4/JHMWU8zwRvxZAh3imRBZgcz4n5rrKyjnUe3A7L8LZcrjbxubVu5IwXtyKecolAR77wsBqkImEIFJ2akcApW';
    //public $merchantPrivateKey = 'MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIJa0URRGp3HFi+/q0/aO1kopMcK9BleJ6SYJ0O3VSh4NUe0GFnqViBIFzcUc/OohWS9zFQBT36akgkc3xNGT1OyLhkixvbkeKEh5dyCG+2Wa+ixESf3zZtjXBMTZUSg+K25SF09HJ07gQ/knly/LuJNsDYc54IkvUjYJsuDCzDLAgMBAAECgYBD1nN1dCe/2G3JZ5wFsHKWUbp/A0pJMWC8HZrBeNVOfFBuAmFx2av8nMfIGFLHiPnylXuSdCQkpNg++dF1vG9nQHFU1grS5meJkF+UDEnK6nHDHTcUbCrssqaLImgs+nDlh1999fABkfSOqx3WVpKS09UE9+KvwqVCSsB9ZR2ZwQJBAO1VTZto3mYrQs6NorWOQ4k+qL3W1II92qi47yx9qqaDEsMpLpk9uWOlkGVkEQIzKWPB0al2j4McKpGhB4fC/PECQQCMm4GhGciuJGp4kNzrsINdHFWmcJy5OcHrDoXZKKsOslBsuWArR4MyMuSyCiQxh5+0n+slovDChy96qx1Gqjl7AkAp/zdNceAw+iTHQj7xm6wNxD294h8WsPVZd+gy2kXxUy5lxIrPaSG+JVUI7A0qc302ftyn/SfmDnzl6jXX60QxAkBXl8yiish8y8zUPRKTdUvwpmUpIcBQl42ZnFt/1Bj0N9toT/d8Hrm5fQEBdNXlUSMJyBIaSS6JUOQgHBdJ3WOfAkBmGQC6f/S+kDzzwn8GtnCAttjfBf1azz8m8LJMxa+rbwFdgt62QGlmFhnQ5dlegOwsWuHvQibyikVAetiMbH56';
      #商户公钥：
    public $merchantPublicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCCyXJ37B9/m/wsBpBYuWXMNNttJKHVeQbV0QAmeV//vUFFYuRlK6F8y8msMV6oagxYOM0FArabUwEkVuDsybST/D6HAAUrJsXOG1W3kCpa3SP/XyF1ZVmWF5HjLl33S+oP0WNJ7HHBe8LoGnrmapxAtDC0FrIuhh+Mvmt977Y2WwIDAQAB';
    //public $merchantPublicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCCWtFEURqdxxYvv6tP2jtZKKTHCvQZXiekmCdDt1UoeDVHtBhZ6lYgSBc3FHPzqIVkvcxUAU9+mpIJHN8TRk9Tsi4ZIsb25HihIeXcghvtlmvosREn982bY1wTE2VEoPituUhdPRydO4EP5J5cvy7iTbA2HOeCJL1I2CbLgwswywIDAQAB';

#易宝公钥：
    public $yeepayPublicKey ='MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCWpHeNeYklKokHLsCMjjxY/y1COFkzkDpwXYSDzh3DEBLeE4wGg6TK5uRcIffgoQXjbKqfOsYmPH88ZxQJW/LOvaiW1h6R4dXsCNQu8El2TyS2dDz82wP1K7rqGF48XgqNTn4MT8RTp6Fb85Ct9OuYsIt+EGBAmBqvcm/+rZ8U9QIDAQAB';
    //public $yeepayPublicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCE8eU1j4WJe2yHU73k8NDEbhQ7Cl24CRd+675fFJ+B6hi4NQlAeqS3JZLQGhEDmVHGgAz9S6l/hYT2UnGTqz0fTBwpIol8FhSQ670AxANrLDXK4h6bicRK5kkFhoEEhWLxCXIO4pWrE6Acu1sDb7SD9j06YU9iWzD4UiT9BQzxRQIDAQAB';
    private $callbackurl = 'http://api1.huoyanzichan.com/Home/Pay/YibaoPayNotice';
    private $newcallbackurl = 'http://api1.huoyanzichan.com/Home/Pay/YibaoNewPayNotice';
    /**
     * 生成请求号
     * @param $length
     * @return string
     */
    public  function random_str($length)
    {
        //生成一个包含 大写英文字母, 小写英文字母, 数字 的数组
        $arr = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
        $str = '';
        $arr_len = count($arr);
        for ($i = 0; $i < $length; $i++)
        {
            $rand = mt_rand(0, $arr_len-1);
            $str.=$arr[$rand];
        }
        return $str;
    }
    public  function  callBackYibao($data,$encryptkey){
        Vendor('yibao.yeepay.yeepayMPay');
        $yeepay = new \yeepayMPay($this->merchantno,$this->merchantPublicKey,$this->merchantPrivateKey,$this->yeepayPublicKey);
        
        $return = $yeepay->callback($data, $encryptkey); //解密易宝支付回调结果
        return $return;
    }
    /**
     * 有短信绑卡请求
     */
    public function sendBindCardRequset($data){
        Vendor('yibao.yeepay.yeepayMPay');
        $yeepay = new \yeepayMPay($this->merchantno,$this->merchantPublicKey,$this->merchantPrivateKey,$this->yeepayPublicKey);
        $requestno               =  $this->random_str(16);
        $identitytype            =  'USER_ID';
        $identityid              =  trim($data['identityid']);
        $cardno                  =  trim($data['cardno']);
        $idcardtype              =  'ID';
        $idcardno                =  trim($data['idcardno']);
        $username                =  trim($data['username']);
        $phone                   =  trim($data['phone']);
        $advicesmstype           =  trim(empty($_POST['advicesmstype'])? MESSAGE : $_POST['advicesmstype']);
        $customerenhancedtype    =  trim($_POST['customerenhancedtype']);
        $avaliabletime           =  trim(empty($_POST['avaliabletime'])? 1440 : $_POST['avaliabletime']);
        $callbackurl             =  trim($_POST['callbackurl']);
        $requesttime             =  date('Y-m-d H:i:s',time());
        $data = $yeepay->bindCardRequest($requestno,$identityid,$identitytype,$cardno,$idcardtype,$idcardno,$username,$phone,
            $advicesmstype,$customerenhancedtype,$avaliabletime,$callbackurl,$requesttime);
        return $data;
    }
    /**
     * 有短验绑卡请求确认
     */
    public function sendBindCardConfirm($data){
        Vendor('yibao.yeepay.yeepayMPay');
        $yeepay = new \yeepayMPay($this->merchantno,$this->merchantPublicKey,$this->merchantPrivateKey,$this->yeepayPublicKey);
        $requestno    = trim($data['requestno']);//绑卡请求号
        $validatecode = trim($data['validatecode']);//短信验证码
        $data = $yeepay->bindCardConfirm($requestno,$validatecode);
        return $data;
    }
    public  function  yibaoSearch($requestno,$yborderid){
        Vendor('yibao.yeepay.yeepayMPay');
        $yeepay = new \yeepayMPay($this->merchantno,$this->merchantPublicKey,$this->merchantPrivateKey,$this->yeepayPublicKey);
        $data = $yeepay->bindPayRecord($requestno,$yborderid);
        return $data;
    }
    /**
     * 无短信绑卡请求
     * @param $data
     * @return \type
     */
    public function nosendBindCardRequset($data){
        Vendor('yibao.yeepay.yeepayMPay');
        $yeepay = new \yeepayMPay($this->merchantno,$this->merchantPublicKey,$this->merchantPrivateKey,$this->yeepayPublicKey);
        $requestno               =  $this->random_str(16);
        $identitytype            =  'USER_ID';
        $identityid              =  trim($data['identityid']);
        $cardno                  =  trim($data['cardno']);
        $idcardtype              =  'ID';
        $idcardno                =  trim($data['idcardno']);
        $username                =  trim($data['username']);
        $phone                   =  trim($data['phone']);
        $requesttime             =  date('Y-m-d H:i:s',time());
        $data = $yeepay->nobindCardRequest($requestno,$identityid,$identitytype,$cardno,$idcardtype,$idcardno,$username,$phone,$requesttime);
        return $data;
    }
    public  function  WithdrawalsMoneyNotice(){
        $strPost = file_get_contents('php://input');  //抓取数据
        Log::pay_write('***用户提现结果回调'.$strPost);
    }
    /**
     *（易宝） 无短信充值 （代扣）
     * return
     * merchantno 商户编号
     *  requestno
     *  yborderid 银生宝唯一流水号
     *  amount
     *  status  PROCESSING：处理中
     *          PAY_FAIL：支付失败
     *          TIME_OUT：超时失败
     *          FAIL：系统异常
     *  errorcode
     *  errormsg
     *  sign
     */
    public function BindPayDirect($identityid,$cardtop,$cardlast,$amount,$requestno){
        Vendor('yibao.yeepay.yeepayMPay');
        $yeepay = new \yeepayMPay($this->merchantno,$this->merchantPublicKey,$this->merchantPrivateKey,$this->yeepayPublicKey);
        $identitytype            =  'USER_ID';
        //$identityid              =  '121221';
        //$amount                  =  trim($data['amount']);
        //$amount                  =  0.01;
        $productname              =  'ID';
        //$cardtop                =  '621799';
        //$cardlast                =  '7440';
        //$callbackurl             =  $this->callbackurl;
        $terminalid                 = 'sasa';
        $lastloginterminalid             = '127.0.0.1';
        $registtime              =   date('Y-m-d H:i:s',time());;
        $lastlogintime              =   date('Y-m-d H:i:s',time());;
        $avaliabletime           =  trim(empty($_POST['avaliabletime'])? 1440 : $_POST['avaliabletime']);
        //$callbackurl             =  trim($_POST['callbackurl']);
        $registip            =  '127.0.0.1';
        $lastloginip            =  '127.0.0.1';
        $issetpaypwd            =  0;
        $free1            =  0;
        $free2            =  0;
        $free3            =  0;
        $requesttime             =  date('Y-m-d H:i:s',time());
        $data = $yeepay->BindPayDirect($requestno,$identityid,$identitytype,$requesttime,$amount,$productname,$avaliabletime,$cardtop,
                                        $cardlast,$this->callbackurl,$terminalid,$registtime,$registip,$lastloginip,$lastlogintime,
                                        $lastloginterminalid,$issetpaypwd,$free1,$free2,$free3);
        Log::info_log('易宝代扣--BindPayDirect-'.'$requestno'.$requestno.'$identityid'.$identityid.'$identitytype'.
                ''.$identitytype.'$requesttime'.$requesttime.'$amount'.$amount.'$productname'.$productname.'$avaliabletime'.$avaliabletime.'$cardtop'.
                $cardtop.'$cardlast'.$cardlast.'callbackurl'.$this->callbackurl.'$terminalid'.$terminalid.'$registtime'.$registtime.
                '$registip'.$registip.'$lastloginip'.$lastloginip.'$lastlogintime'.$lastlogintime.
                '$lastloginterminalid'.$lastloginterminalid.'$issetpaypwd'.$issetpaypwd.'$free1'.$free1.'$free2'.$free2.'$free3'.$free3);
        return $data;
    }
    public function BindNewPayDirect($identityid,$cardtop,$cardlast,$amount,$requestno){
        Vendor('yibao.yeepay.yeepayMPay');
        $yeepay = new \yeepayMPay($this->merchantno,$this->merchantPublicKey,$this->merchantPrivateKey,$this->yeepayPublicKey);
        $identitytype            =  'USER_ID';
        //$identityid              =  '121221';
        //$amount                  =  trim($data['amount']);
        //$amount                  =  0.01;
        $productname              =  'ID';
        //$cardtop                =  '621799';
        //$cardlast                =  '7440';
        //$callbackurl             =  $this->callbackurl;
        $terminalid                 = 'sasa';
        $lastloginterminalid             = '127.0.0.1';
        $registtime              =   date('Y-m-d H:i:s',time());;
        $lastlogintime              =   date('Y-m-d H:i:s',time());;
        $avaliabletime           =  trim(empty($_POST['avaliabletime'])? 1440 : $_POST['avaliabletime']);
        //$callbackurl             =  trim($_POST['callbackurl']);
        $registip            =  '127.0.0.1';
        $lastloginip            =  '127.0.0.1';
        $issetpaypwd            =  0;
        $free1            =  0;
        $free2            =  0;
        $free3            =  0;
        $requesttime             =  date('Y-m-d H:i:s',time());
        $data = $yeepay->BindPayDirect($requestno,$identityid,$identitytype,$requesttime,$amount,$productname,$avaliabletime,$cardtop,
            $cardlast,$this->newcallbackurl,$terminalid,$registtime,$registip,$lastloginip,$lastlogintime,
            $lastloginterminalid,$issetpaypwd,$free1,$free2,$free3);
        Log::info_log('易宝代扣--BindPayDirect-'.'$requestno'.$requestno.'$identityid'.$identityid.'$identitytype'.
            ''.$identitytype.'$requesttime'.$requesttime.'$amount'.$amount.'$productname'.$productname.'$avaliabletime'.$avaliabletime.'$cardtop'.
            $cardtop.'$cardlast'.$cardlast.'callbackurl'.$this->newcallbackurl.'$terminalid'.$terminalid.'$registtime'.$registtime.
            '$registip'.$registip.'$lastloginip'.$lastloginip.'$lastlogintime'.$lastlogintime.
            '$lastloginterminalid'.$lastloginterminalid.'$issetpaypwd'.$issetpaypwd.'$free1'.$free1.'$free2'.$free2.'$free3'.$free3);
        return $data;
    }

}